package tictactoe.game;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;


public class CvC extends AbstractController
{
	 HashMap<int[][],Pair> m = new HashMap<int[][],Pair>();
	 Pair t;
	 public static int retX = 0,retY = 0;
	 private int row,col;
	 int[][] copy;
	    
	 //constructor,initialise grid
	 public CvC(Model model) 
	 {
	    super(model);
	 }
	 
	 //ask the corresponding player for choice
	 //player 1 - computer 1
	 //player 2 - computer 2
	 public void play(int player)
	 {
		 firstMove();
	 }
	 
	 public void firstMove()
	 {
		 Random rand = new Random();
		 int x = rand.nextInt(3)+1;
		 int y = rand.nextInt(3)+1;
		 
		 grid[x-1][y-1] = player+1;
		 retX = x-1;
		 retY = y-1;
		 //show();
		 player ^= 1;
	 }
	 

	 //make move if ok, otherwise ask again for choice of position
	 //check if game finished
	 public int makeMove(int row, int col, int sign) 
	 {
	    if (player == 0)
	    {
	    	copy = grid;
	    	cMove(copy,0);
	    }
	    else
	    {
	    	copy = grid;
	    	cMove(copy,1);
	    }
	    t = m.get(grid);
    	
    	grid[t.f][t.s] = player+1;
    	retX = t.f;
    	retY = t.s;
	   	//show();
	    if (checkWon(grid) == true) 
	    { 
	   	  
	      return player+1;
	    }
	    else
	    if (all==true) { return 3;}
	      
	    player ^= 1;
	    
	    return 0;
	 }

	 
	 
	 
	 private boolean checkForTwoInARow(int[][] board, int sign)
	 {
		 int br = 0;
		 for(int i=0;i<X;i++)
		 {
			 br = 0;
			 for(int j=0;j<Y;j++)
				 if (board[i][j] > 0) br++;
			 if (br == X) continue;
			 br = 0;
			 for(int j=0;j<Y;j++)
				 if (sign == board[i][j]) br++;
				 else {row = i;col = j;}
			 if (br == X-1) break;
		 }
		 if (br == X-1) return true;
		 
		 for(int j=0;j<Y;j++)
		 {
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (board[i][j] > 0) br++;
			 if (br == X) continue;
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (sign == board[i][j]) br++;
				 else {row = i;col = j;}
			 if (br == X-1) break;
		 }
		 if (br == X-1) return true;
		 
		 br = 0;
		 for(int i=0;i<X;i++)
			 if (board[i][i] > 0) br++;
		 if (br < X)
		 {
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (sign == board[i][i]) br++;
				 else {row = i;col = i;}
			 if (br == X-1) return true;
		 }
		 
		 br = 0;
		 for(int i=0;i<X;i++)
			 if (board[i][X-i-1] > 0) br++;
		 if (br < X)
		 {
			 br = 0;
			 for(int i=0;i<X;i++)
				 if (sign == board[i][X-i-1]) br++;
				 else {row = i;col = X-i-1;}
			 if (br == X-1) return true;
		 }
		 return false;
	 }
	 
	 private boolean allUsed(int[][] board)
	 {
		 for(int i=0;i<X;i++)
		 {
			 for(int j=0;j<Y;j++)
				 if (board[i][j] < 0) return false; 
		 }
		 return true;
	 }
	 
	 private ArrayList<Pair> next_states(int[][] board)
	 {
		 ArrayList<Pair> ret = new ArrayList<Pair>();
		 for(int i=0;i<X;i++)
			 for(int j=0;j<Y;j++)
				 if (board[i][j] < 0)
				 {
					 
					 ret.add(new Pair(i,j));
					 
				 }
		 return ret;
	 }
	 
	 private int cMove(int[][] board, int curr_player)
	 {
		int win = -100;
		int x,temp;
		
		if (checkWon(board))
		{
			if 
			(curr_player == player) return 1;
			else return -1;
		}
		else
		if (allUsed(board)) return 0;
		
		ArrayList<Pair> nexts = next_states(board);
		int i,k = nexts.size();
		
		if (checkForTwoInARow(board,player+1))
		{
			m.put(board, new Pair(row,col));
			win = 3*k;
			return win;
		}
		if (checkForTwoInARow(board,(player == 0)? 2 : 1))
		{
			m.put(board, new Pair(row,col));
			win = 3*k;
			return win;
		}
		
		for(i=0;i<=k-1;i++)
		{
			temp = board[nexts.get(i).f][nexts.get(i).s];
			board[nexts.get(i).f][nexts.get(i).s] = curr_player+1;
			x = cMove(board,curr_player^1);
			board[nexts.get(i).f][nexts.get(i).s] = temp;
			
			if (curr_player == player) 
			{
				if (x > win)
				{
					win = x;
					m.put(board,new Pair(nexts.get(i).f,nexts.get(i).s));
				}
			}
			else win = Math.min(win, x);
		}
		
		return win;
	 }
	 
	 
	 
	 
	
	 
	 /*public static void main(String[] args) 
	 {
		 CvC game = new CvC();
	     game.play(0);
	 }
	 */
}